package com.lanou.controller;

import com.lanou.bean.LayUiTable;
import com.lanou.bean.Permission;
import com.lanou.bean.User;
import com.lanou.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/delect")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    @ResponseBody
    @RequestMapping("/PermissionController")
    public int delectPermission(Integer pid){
        return permissionService.delectPermission(pid);
    }

    @ResponseBody
    @RequestMapping("/listPermisssions")
    public Map<String, Object> listpermissions(Model model){
        Map<String, Object> objectObjectMap = new HashMap<>();
        List<Permission> permissions = permissionService.selectPermissons();
        objectObjectMap.put("data",permissions);
        objectObjectMap.put("code",0);
        return objectObjectMap;
    }

    @ResponseBody
    @RequestMapping("/listPermisssion")
    public Map<String, Object> listpermission(Model model, Permission permission){

        Map<String, Object> objectObjectMap = new HashMap<>();
        List<Permission> permissions = permissionService.selectPermisson(permission);
        objectObjectMap.put("listPermissions",permissions);
        objectObjectMap.put("code",0);
        return objectObjectMap;
    }

    @ResponseBody
    @RequestMapping("/listuser")
    public LayUiTable<Permission> queryUsers(Model model,
                                             @RequestParam(value = "page",required = false, defaultValue = "1")Integer currentPage,
                                             @RequestParam(value = "limit",required = false, defaultValue = "10")Integer pageSize,
                                             @RequestParam(required = false) User user){
        return permissionService.queryBooksForLayUI(null,pageSize,currentPage);
    }

    @ResponseBody
    @RequestMapping("/insertpermission")
    public int insertpermisssion(Integer pFather,String pName,String pUrl){
        Permission permission = new Permission();
        permission.setpName(pName);
        permission.setpFather(pFather);
        permission.setpUrl(pUrl);
        return permissionService.insertService(permission);
    }

    @ResponseBody
    @RequestMapping("/insertpermissions")
    public int insertpermisssions(Integer pId, Integer pFather,String pName,String pUrl){
        permissionService.delectPermission(pId);
        Permission permission = new Permission();
        permission.setpId(pId);
        permission.setpName(pName);
        permission.setpFather(pFather);
        permission.setpUrl(pUrl);
        return permissionService.insertService(permission);
    }





}
